<?php
namespace app\backstage\logic;
use think\Db;
class UserRechargeAmountReport
{
    /*列表数据获取*/
    public function getListRank($params)
    {
        $field = 'uid,SUM(money) as money';

        $groupBy = 'uid';
        $type = 2;
        $where = '1=1';
        $uid = request()->_user->id;

        if (request()->_role->alias == 'agent') {
            $type = 1;
            $where .= " AND agent_id = {$uid}";
        }

        if (request()->_role->alias == 'channel') {
            $where .= " AND channel_id = {$uid}";
        }

        if (request()->_role->alias == 'eb') {
            $type = 1;

            $Account = \think\facade\App::model('Account', 'logic');
            $agents = $Account->getAgents(request()->_user->id);
            $agent_id_gh = $agents ? array_column($agents,'id') : [999999999];
            $agent_id_gh = implode(',',$agent_id_gh);
            $where .= " AND agent_id in( {$agent_id_gh})";
        }

        $orderBy = 'a.money DESC';

        $UserRechargeAmountReport = \think\facade\App::model('UserRechargeAmountReport');
        $list = $UserRechargeAmountReport->getListRank($type, $where, $field, $params['page'], $params['limit'], $orderBy, $groupBy);

        if ($list) {

            $uid_gh = array_unique(array_column($list,'uid'));
            $users = Db::table('quick_user')->where('id','in',$uid_gh)->column('id,source_quick_id,regtime');

            foreach ($list as $k => $v) {
                $nickname = Db::table('quick_user_'.$users[$v['uid']]['source_quick_id'])->where('id',$v['uid'])->value('nickname');
                $list[$k]['nickname'] = $nickname;
                $list[$k]['create_time'] = isset($users[$v['uid']]) ? $users[$v['uid']]['regtime'] : '';
                $list[$k]['money'] = $v['money'] > 0 ? round($v['money']/100,2) : 0;
            }
        }

        return $list;
    }

}